/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities1.InvitationRequest;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author takamflore
 */
@Stateless
public class InvitationDaoImpl implements InvitationDao {
   @PersistenceContext(unitName = "meteoCal_PU")
   private  EntityManager em;
   List<InvitationRequest> invitations;
   InvitationRequest invitation;
   
  @Override
  public void addInvitation(InvitationRequest invitation){
      em.persist(invitation);
  } 
  @Override
 public List<InvitationRequest> getAllInvitationUser(int id){
     invitations= em.createNamedQuery("InvitationRequest.findAll").getResultList();
     for(InvitationRequest C:invitations){
            if(C.getUser().getId()!=id){
              try {
            invitations.remove(C);
        } catch (UnsupportedOperationException uoe) {
           // invitations = new ArrayList<InvitationRequest>(invitations);
            invitations.remove(C);
        }
     }
     }
 return invitations;
 }
 @Override
 public List<InvitationRequest> getAllInvitation(){
     invitations= em.createNamedQuery("InvitationRequest.findAll").getResultList();
     return invitations;
 }
 
 @Override
 public void acceptInvitation(InvitationRequest invitation){
     invitation.setAccepted(true);
     em.merge(invitation);
 }
 @Override
 public void declineInvitation(InvitationRequest invitation){
      invitation.setAccepted(false);
     em.merge(invitation);
 }
  @Override
  public InvitationRequest getInvitation(int invitId){
       invitation=em.find(InvitationRequest.class, invitId);
  return invitation;
  }
  
}
